package plagiarism;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import words.ArticleResult;

// ResultManager, called by Big Controller to calculate the similar of different article, and return the rate integer.
public class ResultManager {
	
	private ArrayList<ArticleResult> articleresults;

	// Constructor, store the arraylist from Big Controller
	public ResultManager(ArrayList<ArticleResult> articleresults){
		this.articleresults = articleresults;
		output();
	}

	// Function to create the result index for user.
	public void createIndex(){
		PrintWriter out = null;
		
		
		try {
		out = new PrintWriter(new FileWriter("Index.html"));
		out.append("<html>");
		out.append("<head><title>Index</title></head>");
		out.append("<body>");
		out.append("<h1 style=\"text-align:center\">Plagiarism Checking System!</h1>");
		out.append("<h3 style=\"text-align:left\">Click on the Article result that you want to see!!</h3>");
		for(int i = 1; i < articleresults.size() ; i++)
		{
			out.append("<a href=\"ArticleResult" +(i) +".html\"> Article " + (i) + " Result</a>");
			out.append("<br>");
		}
		out.append("</body>");
		out.append("</html>");
		} catch (IOException e){}finally { out.close();}
	}
	
	// Function to create a new html file for each article, exclude the last article
	public void output(){
		createIndex();	
		for (int i = 0; i < articleresults.size()-1 ; i++) {
			String filename = "ArticleResult" + (i+1) + ".html";
			PrintWriter out = null;
			try {
				out = new PrintWriter(new FileWriter(filename));
				out.append("<html>");
				out.append("<head><title>ArticleResult"+(i+1)+"</title></head>");
				out.append("<body>");
				out.append("<h1 style=\"text-align:center\">Plagiarism Checking System!</h1>");
				out.append("<h2 style=\"text-align:center\">Similar result compare with Article" + (i+1) +  "</h2>");
				ArticleResult articleresult = articleresults.get(i);
				articleresult.output_file(out, i);
				out.append("<br><br><br>");
				out.append("<a href=\"Index.html\"> Return");
				out.append("</body>");
				out.append("</html>");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				out.close();
			}
	
	
		}
	}
}